package com.learn.algorithm.recursive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode90 {

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        f(nums, 0, new int[nums.length], 0, result);
        return result;
    }

    public static void f(int[] nums, int i, int[] path, int size, List<List<Integer>> result) {
        if (i == nums.length) {
            List<Integer> one = new ArrayList<>();
            for (int j = 0; j < size; j++) {
                one.add(path[j]);
            }
            result.add(one);
        } else {
            int j = i + 1;
            while (j < nums.length && nums[j] == nums[i]) {
                j++;
            }
            // 选0个nums[i]
            f(nums, j, path, size, result);
            for (; i < j; i++) {
                path[size++] = nums[i];
                f(nums, j, path, size, result);
            }
        }
    }
}
